// subcontract/Mine/userinfo/userinfo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    userinfo: getApp().userinfo,
    sexShow: false,
    actionsSex: [
      {
        name: '未知',
        subname: '不在显示性别',
        id: 0
      },
      {
        name: '男',
        id: 1
      },
      {
        name: '女',
        id: 2
      },
    ],
    currentDate: new Date().getTime(),
    minDate: 181151656654,
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    datetimeShow: false,
    timestampToDate: getApp().timestampToDate
  },
  blur() {
    this.setUserInfo('nickname', this.data.userinfo.nickname)
  },
  inputonCheng(event) {
    console.log(event);
    this.data.userinfo.nickname = event.detail.value
    this.setData(
      { userinfo: this.data.userinfo }
    )
  },
  /**获取用户信息 */
  async getuserInfo() {
    const { code, data } = await getApp().api.getuserInfo()
    if (code == 1) {
      this.setData({
        userinfo: data
      })
    }
  },
  /**打开弹框 */
  openPopuo(event) {
    console.log(event);
    let { type } = event.currentTarget.dataset
    console.log(type);
    if (type == 'sex') {
      this.setData({
        sexShow: true
      })
    } else {
      this.setData({
        datetimeShow: true
      })
    }
  },
  onClose() {
    this.setData({ sexShow: false, datetimeShow: false });
  },
  confirm(event) {
    this.data.userinfo.birthday = this.data.timestampToDate(event.detail, 'Y-m-d')
    this.setData(
      { userinfo: this.data.userinfo }
    )
    this.setUserInfo('birthday', event.detail)
    this.setData({ datetimeShow: false });
  },
  onSelect(event) {
    console.log();
    this.data.userinfo.sex = event.detail.name
    this.setData(
      { userinfo: this.data.userinfo }
    )
    this.setUserInfo('sex', event.detail.id)
  },
  /**
   * 修改头像
   */
  async chooseMedia() {
    const { errMsg, tempFiles } = await wx.chooseMedia({
      mediaType: ['image'],
      count: 1, //默认9
    })
    if (errMsg == 'chooseMedia:ok') {
      this.uploadFilePromise(tempFiles[0].tempFilePath)
    }
  },
  /**
   * @param {Object} url上传  路径
   * 上传头像
   */
  async uploadFilePromise(url) {
    const { code, data } = await getApp().api.uploadFilePromise(url)
    if (code == 1) {
      this.data.userinfo.avatar = data.uri
      this.setData({
        userinfo: this.data.userinfo
      })
      this.setUserInfo('avatar', data.base_uri)
    }
  },
  async setUserInfo(field, value) {
    let object = {
      field,
      value
    }
    const { code, data } = await getApp().api.setUserInfo(object)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getuserInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})